Plotly surface render test¶

Quick tests for plotly compatibility.

For JupyterLab, need additional extensions - see https://plotly.com/python/getting-started/#jupyterlab-support:

  • conda install -c conda-forge -c plotly jupyter-dash
  • jupyter labextension install jupyterlab-plotly

In some cases may get partially working installation with, e.g., blank surface plots, or plots via HV only. This usually means JupyterLab needs a restart (and maybe a rebuild). For more see https://plotly.com/python/troubleshooting/

Plotly example¶

From https://plotly.com/python/3d-surface-plots/#passing-x-and-y-data-to-3d-surface-plot

On Javascript error "t is null". try a browser refresh and/or Jupyterlab restart.

In [1]:
import plotly.graph_objects as go
import pandas as pd
import numpy as np
# Read data from a csv
z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')
z = z_data.values
sh_0, sh_1 = z.shape
x, y = np.linspace(0, 1, sh_0), np.linspace(0, 1, sh_1)
fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
fig.update_layout(title='Mt Bruno Elevation', autosize=False,
                  width=500, height=500,
                  margin=dict(l=65, r=50, b=65, t=90))
fig.show()
In [2]:
# From https://www.tutorialspoint.com/plotly/plotly_3d_scatter_and_surface_plot.htm
# Also empty
import numpy as np
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T # transpose
z = np.cos(x ** 2 + y ** 2)
trace = go.Surface(x = x, y = y, z =z )
data = [trace]
layout = go.Layout(title = '3D Surface plot')
fig = go.Figure(data = data)
# iplot(fig)
fig.show()

Holoviews 3D surface test¶

From: https://holoviews.org/gallery/demos/plotly/surface_3d.html

See also

  • http://holoviews.org/reference/elements/matplotlib/Surface.html
  • http://holoviews.org/reference/elements/plotly/Surface.html

This wrapper seems robust, and works even when Plotly native rendering is giving issues?

In [3]:
import numpy as np
import holoviews as hv
hv.extension('plotly')

Define data¶

In [4]:
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

surface = hv.Surface(Z, bounds=(-5, -5, 5, 5))

Plot¶

In [5]:
surface.opts(colorbar=True, width=500, height=500)
Out[5]:

Plotting with coords¶

BUT only supports regularly gridded coord systems (not x,y as grids).

In [7]:
xs = np.arange(-4, 4, 0.25)
ys = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(xs, ys)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

surface = hv.Surface((xs, ys, Z))

surface.opts(cmap='fire', height=500, width=500)
Out[7]:

Versions¶

In [6]:
import scooby
scooby.Report(additional=['xarray', 'jupyter','plotly','holoviews'])
Out[6]:
Fri Apr 01 13:27:11 2022 UTC
OS Linux CPU(s) 32 Machine x86_64 Architecture 64bit
RAM 50.1 GiB Environment Jupyter
Python 3.9.10 | packaged by conda-forge | (main, Feb 1 2022, 21:24:11) [GCC 9.4.0]
xarray 2022.3.0 jupyter Version unknown plotly 5.6.0 holoviews 1.14.8
numpy 1.21.5 scipy 1.8.0 IPython 8.1.1 matplotlib 3.5.1
scooby 0.5.12
In [ ]: